package org.vsg.common.pagination.web.taglibs;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.Tag;
import javax.servlet.jsp.tagext.TagSupport;

import org.vsg.common.pagination.Pagination;
import org.vsg.common.pagination.wrapper.GenericWebPaginationWrapper;
import org.vsg.common.pagination.wrapper.GenericWebSortByPaginationWrapper;

public class FirstPageTag extends TagSupport {
	
	
	public int doStartTag() throws JspException {
		
		PaginationBarTag parent = (PaginationBarTag)this.findAncestorWithClass(this,PaginationBarTag.class);
		
		if (parent == null) {
			throw new JspException("Element \"firstPage\" must set in the element \"paginationBar\"! ");
		}
		
		Pagination page = parent.getPagination();
		
		if (page.getTotalRecord() > 0  && page.getPageIndex() > page.getFirstPageIndex()) {
			pageContext.setAttribute("isFirstPageAvailable", new Boolean(true));
		} else {
			pageContext.setAttribute("isFirstPageAvailable", new Boolean(false));
		}
		
		
		// get the parent pagination
		Object wrapper = pageContext.getAttribute(parent.getId());

		if (wrapper instanceof GenericWebSortByPaginationWrapper) {
			GenericWebSortByPaginationWrapper webWrapper = (GenericWebSortByPaginationWrapper)wrapper;
			pageContext.setAttribute("firstPageUrl",webWrapper.getFirstPageUrl());			
		}
		else if (wrapper instanceof GenericWebPaginationWrapper) {
			GenericWebPaginationWrapper webWrapper = (GenericWebPaginationWrapper)wrapper;
			pageContext.setAttribute("firstPageUrl",webWrapper.getFirstPageUrl());
		}
		
		return Tag.EVAL_BODY_INCLUDE;
	}

	
	
	
	
}
